package com.springmvchomework.dao;

import com.springmvchomework.pojo.Student;
import com.springmvchomework.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 从卿
 * @create 2022/4/12-21:49
 */
public class StudentDaoImpl implements StudentDao {
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    @Override
    public int insertStudent(Student student) {
        int n = 0;
        try {
            conn = JDBCUtils.getConnection();
            //2.定义sql
            String sql = "insert into springmvc.student values (?,?,?)";
            //3.获取执行sql的对象
            pstmt = conn.prepareStatement(sql);
            Object[] prams = {student.getName(), student.getAge(), student.getAddress()};
            for (int i = 0; i < prams.length; i++) {
                pstmt.setObject(i + 1, prams[i]);

            }
            n = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return n;

    }

    @Override
    public int updateStudent(Student student) {
        int n = 0;
        try {
            conn = JDBCUtils.getConnection();
            //2.定义sql
            String sql = "update springmvc.student set address=? where name=? ";
            //3.获取执行sql的对象
            pstmt = conn.prepareStatement(sql);
            Object[] prams = new Object[]{student.getAddress(), student.getName()};
            for (int i = 0; i < prams.length; i++) {
                pstmt.setObject(i + 1, prams[i]);
            }
            n = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return n;
    }

    @Override
    public Student findStuByName(String name) {
        Student stu = null;
        try {
            conn = JDBCUtils.getConnection();
            //2.定义sql
            String sql = "select * from springmvc.student where name = '" + name + "' ";
            //3.获取执行sql的对象
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery(sql);
            if (rs.next()) {

                String address = rs.getString("address");
//封装至对象中
                stu = new Student(name, address);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
                pstmt.close();
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return stu;
    }

    @Override
    public List<Student> selectStu() {
        List<Student> studentList = new ArrayList<Student>();
        try {
            conn = JDBCUtils.getConnection();
            //2.定义sql
            String sql = "select * from  springmvc.student";
            //3.获取执行sql的对象
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery(sql);
            while (rs.next()) {
                String name = rs.getString("name");
                String age = rs.getString("age");
                String address = rs.getString("address");
//封装
                Student stu = new Student(name, age, address);
//                将对象放入集合
                studentList.add(stu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
                pstmt.close();
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return studentList;
    }
}
